﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Customer_PrintList
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "regjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "regjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            txtTitleFilter.Text = Now.ToString("dd/MM/yyyy")
            BindToGrid()
            If Not Session("ThongBaoIn") Is Nothing Then
                btnThongBao.Text = "None"
            End If

        End If
    End Sub
    Private Sub BindToGrid()
        Using data As New VSEntities
            'So ban ghi muon the hien tren trang
            Dim _HotelId As Integer = Session("HotelId")
            Dim _PrintedDate As Date
            If txtTitleFilter.Text.Trim = "" Then
                _PrintedDate = Nothing
            Else
                _PrintedDate = StringToDate(txtTitleFilter.Text)
            End If
            Dim p = (From q In data.Customers Where q.HotelId = _HotelId And q.FromDate = _PrintedDate Order By q.FullName Descending Select q).ToList
            Dim strKey_Name() As String = {"CustomerId", "FullName", "HotelId", "ToDate"}
            'Tong so ban ghi
            If p.Count > 0 Then
                btnPrint.Enabled = True
            Else
                btnPrint.Enabled = False
            End If

            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub



    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click

        Using data As New VSEntities
            'So ban ghi muon the hien tren trang
            Dim _HotelId As Integer = Session("HotelId")
            Dim _PrintedDate As Date
            If txtTitleFilter.Text.Trim = "" Then
                _PrintedDate = Nothing
            Else
                _PrintedDate = StringToDate(txtTitleFilter.Text)
            End If

            Dim p = (From q In data.Customers Where q.HotelId = _HotelId And q.FromDate = _PrintedDate Order By q.FullName Select q).ToList
            Dim strKey_Name() As String = {"CustomerId", "FullName", "HotelId", "ToDate", "CheckOutDate"}
            'Tong so ban ghi

            'Tong so ban ghi
            If p.Count > 0 Then
                btnPrint.Enabled = True
            Else
                btnPrint.Enabled = False
            End If

            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
        End Using

    End Sub


    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = e.Row.RowIndex + 1
            Dim lblFullname As Label = CType(e.Row.FindControl("lblFullname"), Label)
            lblFullname.Text = grdShow.DataKeys(e.Row.RowIndex)("FullName")
            Dim lblToDate As Label = CType(e.Row.FindControl("lblToDate"), Label)
            Dim lblCheckOut As Label = CType(e.Row.FindControl("lblCheckOut"), Label)
            Dim _toDate As DateTime = grdShow.DataKeys(e.Row.RowIndex)("ToDate")
            Dim _CheckOutDate As DateTime = grdShow.DataKeys(e.Row.RowIndex)("CheckOutDate")

            If _toDate <> #1/1/1900# Then
                lblToDate.Text = _toDate.ToString("dd/MM/yyyy")
            End If
            If _CheckOutDate <> #1/1/1900# Then

                lblCheckOut.Text = IIf(_CheckOutDate.ToString("dd/MM/yyyy") <> "01/01/0001", _CheckOutDate.ToString("dd/MM/yyyy"), "")
            End If
        End If
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'update lai printed neu o input ngay in
        If txtTitleFilter.Text = "" Then

        End If
    End Sub

End Class
